package com.tm.d9_bigDecimal;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 2024/5/24 Cjj bigDecimal 7：46
 */
public class demo1 {
    public static void main(String[] args) {
        double a=0.1;
        double b=0.2;
        double c=a+b;
        System.out.println(c);

        BigDecimal a1 = new BigDecimal(Double.toString(a));
        BigDecimal a2 = new BigDecimal(Double.toString(b));

        BigDecimal add = a1.add(a2);//加法
        System.out.println(add);

        System.out.println(a2.subtract(a1));//减肥
        System.out.println(a1.multiply(a2));//乘法
        System.out.println(a1.divide(a2));//除法

        BigDecimal b1 = BigDecimal.valueOf(0.1);
        BigDecimal b2 = BigDecimal.valueOf(0.2);
        BigDecimal b3 = b1.divide(b2, 2, RoundingMode.HALF_UP);//除法
        System.out.println(b3);

        //把DigDecimal类型转换为Double类型
        double v = b3.doubleValue();
        System.out.println(v);
    }
}
